MODULE YOS_SOIL

USE PARKIND1,ONLY : JPIM, JPRB, JPRM

! (C) Copyright 2005- ECMWF.
!
! This software is licensed under the terms of the Apache Licence Version 2.0
! which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
! In applying this licence, ECMWF does not waive the privileges and immunities
! granted to it by virtue of its status as an intergovernmental organisation
! nor does it submit to any jurisdiction.

IMPLICIT NONE

SAVE

!       ----------------------------------------------------------------
!*     ** *YOS_SOIL* SOIL PARAMETERS
!       ----------------------------------------------------------------

TYPE :: TSOIL
INTEGER(KIND=JPIM) :: NSOTY        ! NUMBER OF SOIL TEXTURE TYPE (1-7)

REAL(KIND=JPRB) :: RRCSOIL         ! VOLUMETRIC HEAT CAPACITY OF SOIL
REAL(KIND=JPRB) :: RLAMBDADRY      ! DRY SOIL HEAT CONDUCTIVITY
REAL(KIND=JPRB) :: RLAMSAT1        ! CONSTANT FOR DEFINING SOIL HEAT CONDUCTIVITY
REAL(KIND=JPRB) :: RLAMBDAICE      ! ICE HEAT CONDUCTIVITY
REAL(KIND=JPRB) :: RLAMBDAWAT      ! WATER HEAT CONDUCTIVITY
REAL(KIND=JPRB) :: RKERST1         ! CONSTANT FOR DEFINING SOIL HEAT CONDUCTIVITY
REAL(KIND=JPRB) :: RKERST2         ! CONSTANT FOR DEFINING SOIL HEAT CONDUCTIVITY
REAL(KIND=JPRB) :: RKERST3         ! CONSTANT FOR DEFINING SOIL HEAT CONDUCTIVITY
REAL(KIND=JPRB) :: RSRDEP          ! SURFACE RUNOFF REPRESENTATIVE DEPTH
REAL(KIND=JPRB) :: RSIGORMIN       ! SURFACE RUNOFF OROGRAPHY COEFF (MIN)
REAL(KIND=JPRB) :: RSIGORMAX       ! SURFACE RUNOFF OROGRAPHY COEFF (MAX)
REAL(KIND=JPRB) :: RWB             ! EXPONENT IN SOIL MOISTURE POTENTIAL
REAL(KIND=JPRB) :: RCWPSIS         ! CONSTANT FOR SOIL WATER PROPERTIES (DIFF, COND
                                   ! AND SOIL THERMAL DIFFUSIVITY
REAL(KIND=JPRB) :: RWCONS          ! WATER CONDUCTIVITY AT SATURATION
REAL(KIND=JPRB) :: RWSAT           ! SOIL WATER CONTENT AT SATURATION
REAL(KIND=JPRB) :: RWCAP           ! SOIL WATER CONTENT AT FIELD CAPACITY
REAL(KIND=JPRB) :: RWPWP           ! SOIL WATER CONTENT AT WILTING POINT
REAL(KIND=JPRB) :: RSIMP           ! FACTOR FOR TIMESTEP WEIGHTING IN SOIL EQUATION
REAL(KIND=JPRB) :: RLICE           ! THERMAL CONDUCTIVITY FACTOR OF SNOW/ICE
REAL(KIND=JPRB) :: RGH2O           ! HEAT CAPACITY OF WATER (USED FOR SNOW)
REAL(KIND=JPRB) :: RQWEVAP         ! INVERSE OF (*CWCRIT*-*CWPW*) WHERE
REAL(KIND=JPRB) :: RQWSBCR         ! INVERSE OF CRITICAL WETNESS FOR BARE GROUND
REAL(KIND=JPRB) :: RQSNCR          ! INVERSE OF SNOW MASS per unit area WHEN SNOW
                                   ! COVERS THE GROUND COMPLETELY
REAL(KIND=JPRB) :: RQSNCRINV       ! INVERSE OF RQSNCR
REAL(KIND=JPRB) :: RWLMAX          ! MAXIMUM MOISTURE CONTENT OF THE SKIN RESERVOIR
REAL(KIND=JPRB) :: RPSFR           ! 1./RPSFR IS THE FRACTION OF THE GRID-BOX
                                   ! WHERE THE CONVECTIVE PRECIPITATION FALLS.
REAL(KIND=JPRB) :: RTF1            ! UPPER TEMPERATURE FOR SOIL WATER FREEZING
REAL(KIND=JPRB) :: RTF2            ! LOWER TEMPERATURE FOR SOIL WATER FREEZING
REAL(KIND=JPRB) :: RTF3            ! COEFFICIENT FOR SOIL WATER FREEZING FUNCTION
REAL(KIND=JPRB) :: RTF4            ! COEFFICIENT FOR SOIL WATER FREEZING FUNCTION
REAL(KIND=JPRB) :: RTFREEZSICE     ! Temperature at which sea-ice freezes
REAL(KIND=JPRB) :: RTFREEZSICECEL  ! Temperature at which sea-ice freezes in Celsius
REAL(KIND=JPRB) :: RTMELTSICE      ! Temperature at which sea-ice melts
REAL(KIND=JPRB) :: RTMELTSICECEL   ! Temperature at which sea-ice melts in Celsius
REAL(KIND=JPRB) :: RDARSICE        ! THICKNESS OF ARTIC SEA ICE LAYER
REAL(KIND=JPRB) :: RDANSICE        ! THICKNESS OF ANTARTIC SEA ICE LAYER
REAL(KIND=JPRB) :: RCONDSICE       ! Thermal conductivity of sea ice
REAL(KIND=JPRB) :: RDFSICE         ! THERMAL DIFFUSIVITY OF SEA ICE
REAL(KIND=JPRB) :: RCIMIN          ! MINIMUM VALUE FOR SEA ICE FRACTION
REAL(KIND=JPRB) :: RHOICE          ! DENSITY OF ICE   (KG/M3)
REAL(KIND=JPRB) :: RLAMICE         ! THERMAL CONDUCTIVITY OF ICE  (W/MK)
REAL(KIND=JPRB) :: RHOCI           ! VOLUMETRIC ICE HEAT CAPACITY (J/M3K)
REAL(KIND=JPRB) :: RALFMINSN       ! MINIMUM SNOW ALBEDO
REAL(KIND=JPRB) :: RALFMINPSN      ! MINIMUM SNOW ALBEDO FOR PERENNIAL SNOW
REAL(KIND=JPRB) :: RALFMAXSN       ! MAXIMUM SNOW ALBEDO
REAL(KIND=JPRB) :: RSNPER          ! SNOW MASS per unit area FOR PERENNIAL SNOW
REAL(KIND=JPRB) :: RHOMINSN        ! MINIMUM SNOW DENSITY (KG/M3)
REAL(KIND=JPRB) :: RHOMAXSN        ! MAXIMUM SNOW DENSITY (KG/M3)
REAL(KIND=JPRB) :: RHODELTASN      ! RHOMAXSN - RHOMINSN
REAL(KIND=JPRB) :: RTAUF           ! RELAXATION C FOR SNOW ALBEDO AND DENSITY (1/DAY)
REAL(KIND=JPRB) :: RTAUA           ! RELAXATION C FOR COLD SNOW (1/DAY)
REAL(KIND=JPRB) :: RSFRESH         ! SNOWFALL FOR WHICH ALBEDO IS RESET TO MAX (KG/M2S)
REAL(KIND=JPRB) :: RFRSMALL        ! FRACTIONS OF SNOW LESS THAN 0.1 % IS SET TO ZERO
REAL(KIND=JPRB) :: RFRTINY         ! SECURITY CONSTANT < RFRSMALL
REAL(KIND=JPRB) :: RALAMSN         ! EXPONENT IN SNOW HEAT CONDUCTIVITY
REAL(KIND=JPRB) :: RDSNMAX         ! MAXIMUM SNOW DEPTH (M) USED IN THE THERMAL
                                   ! BUDGET OF THE SNOW TEMPERATURE COMPUTATION
                                   ! (THE MAXIMUM IS IMPOSED TO LIMIT THE THERMAL
                                   ! RESPONSE TIME FOR VERY DEEP SNOW LAYERS IN
                                   ! THE SINGLE LAYER SNOW MODEL).
REAL(KIND=JPRB) :: RRCSICE         ! VOLUMETRIC HEAT CAPACITY OF SEA ICE
REAL(KIND=JPRB) :: RTHRFRTI        ! MINIMUM THRESHOLD FOR TILE FRACTION

LOGICAL :: LEVGEN   ! SWITCH TO ACTIVATE VAN GENUCHTEN
LOGICAL :: LESSRO   ! SWITCH TO ACTIVATE SUB-GRID SURFACE RUNOFF
LOGICAL :: LESN09   ! SWITCH TO ACTIVATE SNOW 2009
LOGICAL :: LESNML   ! SWITCH TO ACTIVATE Multi-layer snow 
LOGICAL :: LESNICE  ! SWITCH TO ACTIVATE Multi-layer snow over sea-ice
LOGICAL :: LESNWD   ! SWITCH TO ACTIVATE SNOW LIQUID WATER DIAGNOSTIC
LOGICAL :: LESNRF   ! RAINF INTERCEPTION IN SNOW
LOGICAL :: LESNRSN  ! NEW SNOW DENSITY PARAMETRIZATION
LOGICAL :: LESNWP   ! LIQUID WATER CONTENT - PROGNOSTIC !
LOGICAL :: LESNFA   ! NEW FOREST ALBEDOS
LOGICAL :: LESNCF   ! NEW SNOW COVER FRACTION
LOGICAL :: LESNAS   ! NEW SNOW ALBEDO PARA.

INTEGER(KIND=JPIM) :: NLEVSN        ! NUMBER OF VERTICAL LAYERS IN SNOW
INTEGER(KIND=JPIM) :: NSNMLWS       ! TYPE OF WARM START TO USE FOR ML5 FC INIT

REAL(KIND=JPRB),ALLOCATABLE :: RCWPSISM(:)  !     RCWPSIS IN VAN GENUCHTEN
REAL(KIND=JPRB),ALLOCATABLE :: RMVGALPHA(:) !     ALPHA-FACTOR IN VAN GENUCHTEN
REAL(KIND=JPRB),ALLOCATABLE :: RWCONSM(:)   !     RWCONS IN VAN GENUCHTEN
REAL(KIND=JPRB),ALLOCATABLE :: RMFACM(:)    !     M-EXPONENT IN VAN GENUCHTEN
REAL(KIND=JPRB),ALLOCATABLE :: RNFACM(:)    !     N-EXPONENT IN VAN GENUCHTEN
REAL(KIND=JPRB),ALLOCATABLE :: RLAMBDAM(:)  !     LAMBDA-FACTOR IN VAN GENUCHTEN
REAL(KIND=JPRB),ALLOCATABLE :: RWSATM(:)    !     RWSAT IN VAN GENUCHTEN
REAL(KIND=JPRB),ALLOCATABLE :: RWCAPM(:)    !     RWCAP IN VAN GENUCHTEN
REAL(KIND=JPRB),ALLOCATABLE :: RWPWPM(:)    !     RWPWP IN VAN GENUCHTEN
REAL(KIND=JPRB),ALLOCATABLE :: RWRESTM(:)   !     RWRST IN VAN GENUCHTEN
REAL(KIND=JPRB),ALLOCATABLE :: RDMAXM(:)    !     MAXIMUM DIFFUSIVITY NEAR SATURATION
REAL(KIND=JPRB),ALLOCATABLE :: RDMINM(:)    !     MINIMUM DIFFUSIVITY NEAR AIR-DRY POINT
REAL(KIND=JPRB),ALLOCATABLE :: RQWEVAPM(:)  !     RQWEVAP IN VAN GENUCHTEN
REAL(KIND=JPRB),ALLOCATABLE :: RQWSBCRM(:)  !     RQWSBCR IN VAN GENUCHTEN
REAL(KIND=JPRB),ALLOCATABLE :: RLAMBDADRYM(:) 
REAL(KIND=JPRB),ALLOCATABLE :: RLAMSAT1M(:)
REAL(KIND=JPRB),ALLOCATABLE :: RRCSOILM(:)

REAL(KIND=JPRB),ALLOCATABLE :: RDAT(:) ! ARRAY OF LAYER THICKNESSES FOR TEMPERATURE
REAL(KIND=JPRB),ALLOCATABLE :: RDAW(:) ! ARRAY OF LAYER THICKNESSES FOR MOISTURE
REAL(KIND=JPRB),ALLOCATABLE :: RDAI(:) ! ARRAY OF LAYER THICKNESSES FOR SEA-ICE

! snow related (new parameterization) constants
REAL(KIND=JPRB) :: RLWCSWEA 
REAL(KIND=JPRB) :: RLWCSWEB
REAL(KIND=JPRB) :: RLWCSWEC
REAL(KIND=JPRB) :: RTEMPAMP
REAL(KIND=JPRB) :: RHOMINSNA
REAL(KIND=JPRB) :: RHOMINSNB
REAL(KIND=JPRB) :: RHOMINSNC
REAL(KIND=JPRB) :: RHOMINSND
REAL(KIND=JPRB) :: RSNDTOVERA
REAL(KIND=JPRB) :: RSNDTOVERB
REAL(KIND=JPRB) :: RSNDTOVERC
REAL(KIND=JPRB) :: RSNDTDESTA
REAL(KIND=JPRB) :: RSNDTDESTB
REAL(KIND=JPRB) :: RSNDTDESTC
REAL(KIND=JPRB) :: RSNDTDESTROI
REAL(KIND=JPRB),ALLOCATABLE :: RSNDTDESTC_ML(:)

REAL(KIND=JPRB) :: RHOMAXSN_NEW

! Snow wind-induced densification parameters
REAL(KIND=JPRB) :: RSNDAMOB
REAL(KIND=JPRB) :: RSNDMOB
REAL(KIND=JPRB) :: RSNDAW
REAL(KIND=JPRB) :: RSNDBW
REAL(KIND=JPRB) :: RSNDKV
REAL(KIND=JPRB) :: RSNDATAUW
REAL(KIND=JPRB) :: RSNDBTAUW
REAL(KIND=JPRB) :: RSNDWCOMPMAX
! Snow solar absorption parameters
REAL(KIND=JPRB) :: SSAG1
REAL(KIND=JPRB) :: SSAG2
REAL(KIND=JPRB) :: SSAG3
REAL(KIND=JPRB) :: SSAGSNSMAX
REAL(KIND=JPRB) :: SSASNEXTMIN
REAL(KIND=JPRB) :: SSASNEXTMAX
REAL(KIND=JPRB) :: SSASNEXTCNST
! New snow heat conductivity parameters
REAL(KIND=JPRB) :: SNHCONDAV
REAL(KIND=JPRB) :: SNHCONDBV
REAL(KIND=JPRB) :: SNHCONDCV
REAL(KIND=JPRB) :: SNHCONDPOV

REAL(KIND=JPRB),ALLOCATABLE :: RLEVSNMIN(:) ! ARRAY OF MINIMUM LAYER THICKNESSES for SNOW
REAL(KIND=JPRB),ALLOCATABLE :: RLEVSNMAX(:) ! ARRAY OF MAXIMUM LAYER THICKNESSES for SNOW
REAL(KIND=JPRB),ALLOCATABLE :: RLEVSNMIN_GL(:) ! ARRAY OF MINIMUM LAYER THICKNESSES for SNOW
REAL(KIND=JPRB),ALLOCATABLE :: RLEVSNMAX_GL(:) ! ARRAY OF MAXIMUM LAYER THICKNESSES for SNOW

LOGICAL :: LESKTI5   ! new skin conductivity computation for tile 5 
LOGICAL :: LESKTI8   ! new skin conductivity computation for tile 5 
LOGICAL :: LESOILCOND ! use new soil conductivity as in fcsurf defined function
LOGICAL :: LEROLAKE   ! runoff as precipitation over resolved lakes 

!! TESTING LOGICALS
LOGICAL :: LEWBSOILFIX   ! If true activate fix to converve water (extract even when RC=0) 
LOGICAL :: LEWBCHECK   ! if true check grid-point water balance closure
LOGICAL :: LEWBCHECKAbort   ! if true abort if grid-point water balance closure is not verified
LOGICAL :: LESNCHECK   ! if true check grid-point snow water/energy
LOGICAL :: LESNCHECKAbort   ! if true abort grid-point snow water/energy closure is not verified 
LOGICAL :: LESNWBCON    ! if true conserver snow water balance (avoid reset of snow in glaciers)

! New soil parameters
REAL(KIND=JPRB) :: RBARPWP     !    Pressure head at permanent wilting point
REAL(KIND=JPRB) :: RVGBARCAP   !    Pressure head at field capacity for van Genuchten
REAL(KIND=JPRB) :: RBARCAP     !    Pressure head at field capacity
REAL(KIND=JPRB) :: RCLU        !    unstressed canopy resis. for pot. evap. over grassland (50 s/m)
REAL(KIND=JPRB) :: RLAMBDAMIN  !    minimum thermal conductivity in vlamsk_mod
REAL(KIND=JPRB) :: RLAMBDAMAX  !    maximum thermal conductivity in vlamsk_mod

REAL(KIND=JPRB) :: RLAMBDRYMA  !    coefficient in RLAMBDADRYM3D calculation 0.135
REAL(KIND=JPRB) :: RLAMBDRYMB  !    coefficient in RLAMBDADRYM3D calculation 64.7
REAL(KIND=JPRM) :: RLAMBDRYMC  !    coefficient in RLAMBDADRYM3D calculation 0.947


! parameters of f1
REAL(KIND=JPRB) :: RRSF1A      !    a in f1 - see documentation p. 131
REAL(KIND=JPRB) :: RRSF1B      !    1/b in f1 (to achive bitwise comparable) - see documentation p. 131
REAL(KIND=JPRB) :: RRSF1C      !    c in f1 - see documentation p. 131
! Parameters from Peters-Lidard 1998
REAL(KIND=JPRB) :: RRHOSM      !    solids unit weight - kg m^-3
REAL(KIND=JPRB) :: RLAMBDAQ    !    therm. cond. quarz - W m^-1 K^-1
REAL(KIND=JPRB) :: RLAMBDAO    !    therm. cond. other - W m^-1 K^-1

! new snow parameters
REAL(KIND=JPRB) :: RSNLARGE ! large number to impose Tsk=SST, see vlamsk_mod
REAL(KIND=JPRB) :: RSNLARGESN ! large number to constrain Tsk variations in case of melting snow
REAL(KIND=JPRB) :: RSNLARGEWT ! large number to constrain Tsk variations in case of melting snow
REAL(KIND=JPRB) :: RSNSNOW
REAL(KIND=JPRB) :: RSNSNOWHVEG
REAL(KIND=JPRB) :: RSNRTTEMP
REAL(KIND=JPRB) :: RSNPERT    ! permanent snow threshold
REAL(KIND=JPRB) :: RSNGLC     ! glacial snow mass
REAL(KIND=JPRB) :: RSMINZ     ! min snow hight - if LESKTI5 (i.e. for tile 5)

! parameter Snow thermal conductivity function
REAL(KIND=JPRB) :: FSNTCONDA  ! coeff in FSNTCOND function
REAL(KIND=JPRB) :: FSNTCONDB  ! coeff in FSNTCOND function
REAL(KIND=JPRB) :: FSNTCONDC  ! coeff in FSNTCOND function

END TYPE TSOIL

END MODULE YOS_SOIL
